/**
 * 注意事项
 * 该页面勿动，如有需要更改请与技术中心前端负责人联系
 */
import Vue from 'vue'
import Vuex from 'vuex'
import getters from './getters'
import gettersFramework from '@/framework/store/getters.js'
import createPersistedState from 'vuex-persistedstate'
// 将vuex本地存储实现
Vue.use(Vuex)

// https://webpack.js.org/guides/dependency-management/#requirecontext
const modulesFiles = require.context('./modules', true, /\.js$/)
const modulesFilesFramework = require.context('@/framework/store/modules', true, /\.js$/) // 处理framework模块modules
// you do not need `import app from './modules/app'`
// it will auto require all vuex module from modules file
// eslint-disable-next-line no-shadow
const modules = modulesFiles.keys().reduce((modules, modulePath) => {
  // set './app.js' => 'app'
  const moduleName = modulePath.replace(/^\.\/(.*)\.\w+$/, '$1')
  const value = modulesFiles(modulePath)
  modules[moduleName] = value.default
  return modules
}, {})
// eslint-disable-next-line no-shadow
const modulesFramework = modulesFilesFramework.keys().reduce((modules, modulePath) => {
  // set './app.js' => 'app'
  const moduleName = modulePath.replace(/^\.\/(.*)\.\w+$/, '$1')
  const value = modulesFilesFramework(modulePath)
  modules[moduleName] = value.default
  return modules
}, {})
const store = new Vuex.Store({
  modules: { ...modules, ...modulesFramework },
  getters: { ...getters, ...gettersFramework },
  plugins: [
    createPersistedState({
      storage: window.localStorage, // localStorage,会话结束缓存清除
      reducer(val) {
        return {
          // 只存储state中的userData
          settings: val.settings
        }
      }
    })
  ]
})

export default store
